﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class NewsMangement : System.Web.UI.Page
{
    News currentNews = new News();
    ArrayList sqlList = new ArrayList();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Session["userName"]!=null)
        {
            if(!IsPostBack)
            {
                BindNews();
            }
        }
        else
        {
            Response.Redirect("~/Login.aspx");
        }

    }
    /// <summary>
    /// 绑定新闻
    /// </summary>
    protected void BindNews()
    {
        DataSet ds = currentNews.ReadRecordNews();
        DataView dv=ds.Tables["news"].DefaultView;
        dv.Sort = " newsID asc";
        dgNewsHistory.DataSource =dv ;
        dgNewsHistory.DataBind();
    }
    /// <summary>
    /// 添加新闻
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        currentNews.NewsTitle = txtNewsTitle.Text.Trim();
        currentNews.NewsContent = txtNewsContent.Text;
        DateTime addTime = DateTime.Now;
        currentNews.AddTime = addTime.ToLongDateString();
        if(currentNews.AddNews())
        {
            
            Response.Write("<script language='javascript'>alert('添加成功！')</script>");
            BindNews();

        }
        else
        {
            Response.Write("<script language='javascript'>alert('在尝试写入数据库时，发生了未知意外，添加失败！请您联系管理员。')</script>");
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtNewsContent.Text = "";
    }
    protected void dgNewsHistory_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        dgNewsHistory.CurrentPageIndex = e.NewPageIndex;
        BindNews();
    }
    /// <summary>
    /// 点击全选的方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void chkAll(object sender, EventArgs e)
    {
        CheckBox chkAll = (CheckBox)sender;
        if(chkAll.Text=="全选")
        {
            foreach(DataGridItem dg in dgNewsHistory.Items)
            {
                CheckBox chk =(CheckBox ) dg.FindControl("chkSingle");
                chk.Checked = chkAll.Checked;
            }
        }
    }
    /// <summary>
    /// 删除新闻的事件
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void dgNewsHistory_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        switch(e.CommandName)
        {
            case "delete":
            if(dgNewsHistory.Items.Count==1)
            {
                if(dgNewsHistory.CurrentPageIndex!=0)
                {
                dgNewsHistory.CurrentPageIndex = dgNewsHistory.CurrentPageIndex - 1;
                }
            }
            int newsID =Convert.ToInt32( e.Item.Cells[0].Text);
            if (currentNews.DeleteNews(newsID))
            {
                BindNews();
                Response.Write("<script language='javascript'>alert('删除成功！')</script>");
              
               
            }
            else 
            {
                Response.Write("<script language='javascript'>alert('在尝试操作数据库时，发生未知意外，删除失败，请您联系管理员！')</script>");
            }
            break;
            case "delSelected":
                if (dgNewsHistory.Items.Count == 1)
                {
                    if (dgNewsHistory.CurrentPageIndex != 0)
                    {
                        dgNewsHistory.CurrentPageIndex = dgNewsHistory.CurrentPageIndex - 1;
                    }
                }

                DelSelected(source,e);
                break;
        }
       
    }
    /// <summary>
    /// 删除被选择的新闻的方法
    /// </summary>
    public void DelSelected(object source, DataGridCommandEventArgs e)
    {
        foreach(DataGridItem dg in dgNewsHistory.Items)
        {
            CheckBox chkBox =(CheckBox) dg.FindControl("chkSingle");
            if(chkBox.Checked==true)
            {
                int newsID = Convert.ToInt32(dg.Cells[0].Text);
                string strDel = "delete from NEWS where newsID="+newsID;
                sqlList.Add(strDel);
            }
        }
        if (currentNews.DeleteNewses(sqlList))
        {
            BindNews();
            Response.Write("<script language='javascript'>alert('删除成功！')</script>");
        }
        else
        {
            Response.Write("<script language='javascript'>alert('在尝试操作数据库时，发生未知意外，删除失败，请您联系管理员！')</script>");
        }
    }
    protected void dgNewsHistory_PageIndexChanged1(object source, DataGridPageChangedEventArgs e)
    {
        dgNewsHistory.CurrentPageIndex = e.NewPageIndex;
        BindNews();
    }
}
